//
//  CourseCollectionViewCell.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/27.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "CourseCollectionViewCell.h"

#import "MoviePlayerViewController.h"

@interface CourseCollectionViewCell ()

@property (nonatomic, strong) UILabel * progressLabel; // 进度
@property (nonatomic, strong) UIButton * startStudyButton; // 开始学习
@property (nonatomic, strong) UIImageView * stopImageView; // 暂停
@property (nonatomic, strong) UIView * blueLineView; // 蓝色线条
@property (nonatomic, strong) UIView * grayLineView; // 灰色线条

@end

@implementation CourseCollectionViewCell

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius = 16;
        self.layer.masksToBounds = YES;
        self.layer.borderColor = RGB_COLOR(176, 176, 176, 1).CGColor;
        self.layer.borderWidth = 1;
        
        // 添加视图
        [self addSubview:self.imageView];
        [self addSubview:self.titleLabel];
        [self addSubview:self.progressLabel];
        [self addSubview:self.startStudyButton];
        [self addSubview:self.blueLineView];
        [self addSubview:self.grayLineView];
        [self addSubview:self.stopImageView];
        
        // 自动布局
        [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self);
            make.height.equalTo(self).multipliedBy(0.75);
            make.centerX.equalTo(self);
            make.top.equalTo(self);
        }];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.bottom.equalTo(_imageView);
        }];
        [_progressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self);
            make.centerY.equalTo(self).multipliedBy(1.62);
        }];
        [_startStudyButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self);
            make.height.equalTo(self).multipliedBy(0.13);
            make.centerX.equalTo(self);
            make.bottom.offset(0);
        }];
        [_stopImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(self.mas_width).multipliedBy(0.07);
            make.centerY.equalTo(_progressLabel);
            make.left.offset(0);
        }];
        [_blueLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.offset(2);
            make.left.offset(8);
            make.right.equalTo(_stopImageView.mas_centerX);
            make.centerY.equalTo(_stopImageView);
        }];
        [_grayLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.offset(2);
            make.right.equalTo(self).multipliedBy(0.82);
            make.left.equalTo(_stopImageView.mas_centerX);
            make.centerY.equalTo(_stopImageView);
        }];
        
        // 延迟实现动画
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self viewAnimation];
        });
    }
    return self;
}

#pragma mark 🌷🌷🌷event responds🌷🌷🌷
- (void)respondsToStartStudyButton {
    _startLearBlock(_videoid);
}

#pragma mark 🌷🌷🌷private methods🌷🌷🌷
- (void)viewAnimation {
    [_stopImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.offset(CGRectGetWidth(self.frame) * 0.8);
    }];
    [UIView animateWithDuration:1 animations:^{
        [self layoutIfNeeded];
    }];
}
- (void)clickBackForStartLearBlock:(StartLearBlock)startLearBlock {
    _startLearBlock = startLearBlock;
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:22];
    }
    return _titleLabel;
}
- (UILabel *)progressLabel {
    if (!_progressLabel) {
        _progressLabel = [[UILabel alloc] init];
        _progressLabel.text = @"100%";
        _progressLabel.textColor = RGB_COLOR(134, 134, 134, 1);
    }
    return _progressLabel;
}
- (UIButton *)startStudyButton {
    if (!_startStudyButton) {
        _startStudyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_startStudyButton setImage:[UIImage imageNamed:@"i_up_开始学习_03.png"] forState:UIControlStateNormal];
        [_startStudyButton addTarget:self action:@selector(respondsToStartStudyButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _startStudyButton;
}
- (UIImageView *)stopImageView {
    if (!_stopImageView) {
        _stopImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"smallPause.png"]];
    }
    return _stopImageView;
}
- (UIView *)blueLineView {
    if (!_blueLineView) {
        _blueLineView = [[UIView alloc] init];
        _blueLineView.backgroundColor = RGB_COLOR(113, 206, 229, 1);
    }
    return _blueLineView;
}
- (UIView *)grayLineView {
    if (!_grayLineView) {
        _grayLineView = [[UIView alloc] init];
        _grayLineView.backgroundColor = RGB_COLOR(149, 149, 149, 1);
    }
    return _grayLineView;
}

@end
